package com.cloudsigns.factorymanager2.project.model;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * 添加批量安装单
 * Created by tony on 2018/3/17.
 */

public class AddEngineeringBatchInstallationOrderModel implements IAddEngineeringBatchInstallationOrderModel {
    @Override
    public void addEngineeringBatchInatallationOrder(final long completeDate, final List<EngineeringInstallationOrderBean> engineeringInstallRequestList, final String projectName, final AddEngineeringBatchInstallationOrderModel.OnAddEngineeringBatchInstallationOrderListener addEngineeringBatchInstallationOrderListener) {
        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.ADD_ENGINEERING_BATCH_INSTALLATION_ORDER_URL);

                HttpResponse response;
                try {
                    // 添加http头信息
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("completeDate", completeDate);

                    JSONArray engineeringInstallRequestListJsonArr = new JSONArray();
                    if (null != engineeringInstallRequestList && engineeringInstallRequestList.size() > 0) {
                        for (EngineeringInstallationOrderBean engineeringBean : engineeringInstallRequestList) {
                            JSONObject engineeringInstallRequestListJsonObj = new JSONObject();
                            engineeringInstallRequestListJsonObj.put("engineeringAddress", engineeringBean.getEngineeringAddress());
                            engineeringInstallRequestListJsonObj.put("engineeringName", engineeringBean.getEngineeringName());
                            engineeringInstallRequestListJsonObj.put("finishDate", completeDate);
                            engineeringInstallRequestListJsonObj.put("lat", engineeringBean.getLat());
                            engineeringInstallRequestListJsonObj.put("lon", engineeringBean.getLon());

                            List<String> picUrlLs = engineeringBean.getPicUrl();
                            JSONArray picUrlJsonArr = new JSONArray();
                            if (null != picUrlLs && picUrlLs.size() > 0) {

                                for (String picpath : picUrlLs) {
                                    picUrlJsonArr.put(picpath);
                                }
                            }
                            engineeringInstallRequestListJsonObj.put("picUrl", picUrlJsonArr);

                            JSONArray productListJsonArr = new JSONArray();
                            List<ProductBean> productBeanLs = engineeringBean.getProductList();
                            if (null != productBeanLs && productBeanLs.size() > 0) {
                                for (ProductBean productBean : productBeanLs) {
                                    JSONObject productListJsonObj = new JSONObject();
                                    productListJsonObj.put("amount", productBean.getAmount());
                                    productListJsonObj.put("id", productBean.getId());
                                    productListJsonObj.put("name", productBean.getName());

                                    JSONArray picUrlJsonArr_prodcut = new JSONArray();
                                    List<String> picUrlLs_prodcut = productBean.getPicUrl();
                                    if (null != picUrlLs_prodcut && picUrlLs_prodcut.size() > 0) {
                                        for (String picurlStr : picUrlLs_prodcut) {
                                            picUrlJsonArr_prodcut.put(picurlStr);
                                        }
                                    }
                                    productListJsonObj.put("picUrl", picUrlJsonArr_prodcut);

                                    JSONObject shapeTypeJsonObj = new JSONObject();
                                    shapeTypeJsonObj.put("border", productBean.getBorder());
                                    shapeTypeJsonObj.put("borderBottom", productBean.getBorderBottom());
                                    shapeTypeJsonObj.put("borderLeft", productBean.getBorderLeft());
                                    shapeTypeJsonObj.put("borderRight", productBean.getBorderRight());
                                    shapeTypeJsonObj.put("borderTop", productBean.getBorderTop());
                                    shapeTypeJsonObj.put("desc", productBean.getDesc());
                                    shapeTypeJsonObj.put("diameter", productBean.getDiameter());
                                    shapeTypeJsonObj.put("height", productBean.getHeight());
                                    shapeTypeJsonObj.put("thick", productBean.getThick());
                                    if (null != productBean.getType()) {
                                        CommonEnum.ProductShapeType type = productBean.getType();
                                        if (type == CommonEnum.ProductShapeType.RECTANGLE) {
                                            shapeTypeJsonObj.put("type", "RECTANGLE");
                                        } else if (type == CommonEnum.ProductShapeType.ROUND) {
                                            shapeTypeJsonObj.put("type", "ROUND");

                                        } else if (type == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                                            shapeTypeJsonObj.put("type", "SPECIALSHAPE");

                                        }
                                    }
                                    shapeTypeJsonObj.put("width", productBean.getWidth());
                                    productListJsonObj.put("shapeType", shapeTypeJsonObj);
                                    productListJsonObj.put("unit", productBean.getUnit());

                                    productListJsonArr.put(productListJsonObj);
                                }
                            }

                            engineeringInstallRequestListJsonObj.put("productList", productListJsonArr);
                            engineeringInstallRequestListJsonArr.put(engineeringInstallRequestListJsonObj);
                        }
                    }

                    jsonObject.put("engineeringInstallRequestList", engineeringInstallRequestListJsonArr);
                    jsonObject.put("projectName", projectName);

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);

                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());

                    httpPost.setEntity(se);

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != addEngineeringBatchInstallationOrderListener) {

                        if (code.equals("0")) {

                            addEngineeringBatchInstallationOrderListener.success(json.getString("id"));
                        } else {

                            addEngineeringBatchInstallationOrderListener.failed(code + "");


                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnAddEngineeringBatchInstallationOrderListener {
        void success(String projectId);

        void failed(String desc);
    }
}
